<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="pd.showSearch">
        <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true" label-width="85px">
          <el-form-item label="物品" prop="productId">
            <ProductSearch v-model:data-text="pd.productName"  :params="{isStock:true}" placeholder="请选择物品" @ok="searchProduct($event)" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="批次号" prop="batchCode">
            <el-input v-model="pd.queryParams.batchCode" placeholder="请输入批次号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="仓库" prop="warehouseId">
            <WarehouseSelect v-model:wh-id="pd.queryParams.warehouseId" placeholder="请选择仓库" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="供应商" prop="supplierId">
            <PartnerSearch v-model:data-text="pd.partnerName" :params="{isSupplier:true}" placeholder="请输入供应商" @ok="searchPartner($event)" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="库存有效期" prop="expireDate">
            <el-date-picker clearable
              v-model="pd.queryParams.expireDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择库存有效期"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table v-loading="pd.loading" :data="pd.stockList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="物品名" align="center" prop="productName" />
        <el-table-column label="物品码" align="center" prop="sku" />
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="单位" align="center" prop="measureUnit" />
        <el-table-column label="批次号" align="center" prop="batchCode" />
        <el-table-column label="仓库" align="center" prop="wareName" />
        <el-table-column label="库位" align="center" prop="areaId" />
        <el-table-column label="供应商" align="center" prop="partnerName" />
        <el-table-column label="生产厂商" align="center" prop="manufacturerId" />
        <el-table-column label="在库数量" align="center" prop="quantity" />
        <el-table-column label="采购单价" align="center" prop="purchasePrice" />
        <el-table-column label="库存有效期" align="center" prop="expireDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>

      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
    </el-card>

  </div>
</template>

<script setup name="Stock" lang="ts">
import {StockVO, listStock } from '@/views/wms/stock/api';
import {ProductVO} from "@/views/wms/product/api";
import ProductSearch from "@/views/wms/product/search.vue";
import WarehouseSelect from "@/views/wms/warehouse/select.vue"
import PartnerSearch from "@/views/hrp/partner/search.vue"
import {PartnerVO} from "@/views/hrp/partner/api";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const queryFormRef = ref<ElFormInstance>();
const stockFormRef = ref<ElFormInstance>();

const pd = reactive({
  stockList: <StockVO[]>([]),
  buttonLoading: false,
  loading: true,
  showSearch: true,
  selectVO: <StockVO[]>([]),
  single: true,
  multiple: true,
  total: 0,
  partnerName: '',
  productName: '',
  form: <StockVO>{},
  queryParams: <StockVO>{
    pageNum: 1,
    pageSize: 10,
    productId: undefined,
    measureUnit: undefined,
    batchCode: undefined,
    warehouseId: undefined,
    areaId: undefined,
    supplierId: undefined,
    manufacturerId: undefined,
    quantity: undefined,
    purchasePrice: undefined,
    expireDate: undefined,
    params: {
    }
  },
  rules: <ElFormRules>{
    productId: [
      { required: true, message: "物品码不能为空", trigger: "blur" }
    ],
    measureUnit: [
      { required: true, message: "单位不能为空", trigger: "blur" }
    ],
    batchCode: [
      { required: true, message: "批次号不能为空", trigger: "blur" }
    ],
    warehouseId: [
      { required: true, message: "仓库不能为空", trigger: "blur" }
    ],
    quantity: [
      { required: true, message: "在库数量不能为空", trigger: "blur" }
    ],
  },
  dialog: <DialogOption>{
    visible: false,
    title: ''
  }
});

// const { queryParams, form, rules } = toRefs(data);

function searchProduct(pVo: ProductVO[]) {
  pVo.forEach((v) => {
    pd.queryParams.productId = v.id;
    pd.productName = v.name!;
    return;
  });
}
function searchPartner(pVo: PartnerVO[]) {
  pVo.forEach((v) => {
    pd.queryParams.supplierId = v.id;
    if (v.name != null) {
      pd.partnerName = v.name
    }
    return;
  });
}

/** 查询库存数量列表 */
async function getList() {
  pd.loading = true;
  const res = await listStock(pd.queryParams);
  pd.stockList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 取消按钮 */
function cancel() {
  reset();
  pd.dialog.visible = false;
}

/** 表单重置 */
function reset() {
  pd.form = <StockVO>{};
  stockFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  pd.partnerName = '';
  pd.productName = '';
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection: StockVO[]) {
  pd.selectVO = selection;
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy?.download('wms/stock/export', {
    ...pd.queryParams
  }, `stock_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
